<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>绑定手机号</title>
    <link rel="stylesheet" type="text/css" href="../css/styleb.css">
    <link rel="stylesheet" type="text/css" href="../css/common.css">
    <style>
        /* 提示框 */
    
        .tip {
            display: none;
            padding: 10px 30px;
            border-radius: 5px;
            background-color: rgba(0, 0, 0, 0.6);
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);
            color: #fff;
            z-index: 999;
        }
    </style>
</head>
<body>
    <div class="set-pwd">
        <div class="login-form">
            <div class="input-row wbox mt18">
                <input maxlength="11" class='mobile' v-model="loginInfo.mobile" type="tel" placeholder="请输入手机号" />
                <span @click="clearVal(1)" class="clear-icon"></span>
            </div>
            <div class="input-row wbox sms-row">
                <input v-model="loginInfo.regMsgCode" type="tel" class='regMsgCode' placeholder="请输入验证码" />
                <span @click="clearVal(2)" class="clear-icon"></span>
                <button type="button" @click="sendCode" ref='sMs' class="sms-btn">获取验证码</button>
            </div>
            <div class="mt18">
                <button type="button" @click="subbind" class="btn-blcok btn-gary radius5 sub">提交</button>
            </div>
            <div class="forget-pwd-text mt30">
                <a href="./bindTelN.html">绑定未注册账号</a>
            </div>
        </div>
        <div class="tip"></div>

        <div class="test1"></div>
        <!-- <div class="test2"></div> -->
    </div>
</body>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="../js/prompt.js"></script>
<script type="text/javascript">
$(function() {
    // alert(localStorage.getItem("fromId"))
    var fromId = JSON.parse(localStorage.getItem("fromId"));
    var initiatorId = fromId.initiatorId || ''
    var isopenPage = fromId.isopenPage || '';
    var from = fromId.spreadId || ''
    // alert(isopenPage)
    if (isopenPage != 0 || isopenPage == '') {
        isopenPage = 1
        // alert(isopenPage)
    }


    // alert(initiatorId)
    var mobile = '', regMsgCode = '';
    //data绑定ajax请求参数
    var subData = {}
    var token = localStorage.getItem("token")||'';
    // alert(token)
    

    // alert(localStorage.getItem("userinfo"))
    var userinfo = JSON.parse(localStorage.getItem("userinfo"))||'';
    if (userinfo.thMemberVo != undefined && userinfo.thMemberVo != null && userinfo.thMemberVo != '') {
        var openId = userinfo.thMemberVo.openId;
    }
    // var openId = userinfo.thMemberVo.openId||'';
    // var openId ='';
    // alert(openId)
    /* 删除按钮展示 */
    $(".mobile").on("input propertychange",function() {
        $(this).next(".clear-icon").css("display", "inline-block");
    })
    $(".mobile").on("blur", function () {
        // $(this).next(".clear-icon").css("display","none");
        mobile = $(".mobile").val();
        subData.mobile = mobile;
        if (mobile != '' && regMsgCode != '') {
            $(".mt18 button").addClass("btn-red");
        }else {
            $(".mt18 button").removeClass("btn-red");
        }
    });
    $(".regMsgCode").on("input propertychange", function () {
        $(this).next(".clear-icon").css("display", "inline-block");
        regMsgCode = $(".regMsgCode").val();
        if (mobile != '' && regMsgCode != '') {
            $(".mt18 button").addClass("btn-red");
        } else {
            $(".mt18 button").removeClass("btn-red");
        }
    })
    /* 删除按钮点击清除 */
    $(".clear-icon").on("click",function() {
        $(this).css("display", "inline-block");
        $(this).prev().val('').focus();
        $(this).hide();
    })
    $(".regMsgCode").on("blur", function () {
        // $(this).next(".clear-icon").css("display", "none");
        regMsgCode = $(".regMsgCode").val();
        subData.regMsgCode = regMsgCode;
        if (mobile != '' && regMsgCode != '') {
            $(".mt18 button").addClass("btn-red");
        } else {
            $(".mt18 button").removeClass("btn-red");
        }
    });
    
     /**
       *验证码提交请求
       *subData.mobile     手机号
       *subData.smsType    验证码类型
       */
    $(".sms-btn").on("click", function () {
        $(this).removeClass("btn-red");
        var that = this;
        if (!(/^1[34578]\d{9}$/.test(subData.mobile))) {
            $(".tip").html("手机号格式不正确").delay(500).show(300).delay(1000).hide(300);
        } else {
            //data绑定ajax请求参数
            subData.smsType = 2;
            $.ajax({
                type: "post",
                // url: url + "/api/sms/yzmMember",
                url: url + "/api/activity/yzmMember",   //更换了接口，5.4
                data: subData,
                async: true,
                success: function (res) {
                    console.log(res)
                    if (res.code == 200) {
                        var sMs = $(that)
                        console.log(sMs)
                        settime(sMs)
                        $(".tip").html("验证码发送成功").show(300).delay(1000).hide(300);
                    } else {
                        $(".tip").html(res.msg).delay(500).show(300).delay(1000).hide(300);

                    }
                }
            })
        }

    })
    // alert('推广id' + from)
    $(".sub").on("click", function () {
        var btnstatus = $(this).hasClass("btn-red");
        if (btnstatus) {
            if (!/^1[34578]\d{9}$/.test(subData.mobile)) {
                $(".tip").html("手机号格式不正确").show(300).delay(1000).hide(300);
                return;
            }
            if (subData.regMsgCode == "") {
                $(".tip").html("验证码不能为空").show(300).delay(1000).hide(300);
                return;
            }
            // var mobile = JSON.parse(subData.mobile)
            // console.log(subData.mobile)
            // console.log(mobileString)
            /**
            *新老会员区别请求
            *subData.mobile     手机号
            */
            $.ajax({
                type: "post",
                url: url + "/api/activity/memberFlag",
                data: subData,
                async: true,
                success: function (res) {
                    console.log(res)
                    if (res.code == 200) {
                        if (res.obj == false) {
                            $(".tip").html("当前手机号未注册，去绑定未注册账号").show(300).delay(1000).hide(300);
                        } else {
                            /**
                           *注册请求
                           *subData.mobile     手机号
                           *subData.smsType    验证码类型
                           *subData.openId    微信openId
                           *subData.regMsgCode    验证码
                           *subData.source    客户端来源
                           *subData.areaId    区域Id
                           *subData.from    推广id
                           *subData.pwd    密码
                           *subData.token    登录凭证token
                           *subData.loginType    登录类型(50-本地会员,51-第三方微信登录,52-第三方QQ登录)
                           */
                            // subData.token = '904b9972be84fde7e8ba3eb48094e6d7';
                            // subData.token = '2d737f0cf754faeb5071199d8e6b4130';
                            subData.token = token;
                            // subData.openId = 'oYAbBwk_ga7-WQ_6TxzV6gxfWO08';
                            // subData.openId = 'oYAbBws_a-IUgtt_BKkcnYIAiWe4';
                            subData.openId = openId;
                            subData.source = 3;
                            subData.areaId = 14;
                            subData.loginType = 51;
                            subData.pwd = 0  //已有账号密码为0
                            subData.from = from;
                            $.ajax({
                                type: "post",
                                url: url + "/api/activity/bind",
                                data: subData,
                                async: true,
                                success: function (res) {
                                    console.log(res)
                                    // alert(JSON.stringify(res))
                                    $(".test1").html(subData)
                                    $(".test2").html(openId)
                                    // alert(res)
                                    if (res.code == 200) {
                                        // alert(JSON.stringify(res.obj))
                                        localStorage.setItem("isbind", "true");
                                        localStorage.setItem('userinfo', JSON.stringify(res.obj));
                                        // alert(localStorage.getItem('userinfo'))
                                        $(".tip").html("绑定成功").delay(500).show(300).delay(1000).hide(300);
                                        window.location.href = './lxIndex.html?isopenPage=' + isopenPage + '&initiatorId=' + initiatorId + '&spreadId=' + from
                                        // window.location.href = './lxIndex.html?isopenPage=0&initiatorId=' + initiatorId
                                    } else {
                                        $(".tip").html(res.msg).show(300).delay(1000).hide(300);
                                    }
                                }
                            })
                        }
                    }
                }
            })
        }
    })


    //短信验证码
    var countdown = 60;
    function settime(obj) {
        if (countdown == 0) {
            obj.removeAttr("disabled");
            obj.html("重新发送");
            countdown = 60;
            return;
        } else {
            obj.attr("disabled", true);
            obj.html("" + countdown + "s");
            countdown--;
        }
        setTimeout(function () {
            settime(obj)
        }, 1000)
    }

})

</script>
</html>